package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Zhang Maojie
 * @date 2024/3/27 22:16
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        ArrayList<Double> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            HashMap<Object, Object> map = new HashMap<>();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status", Orders.COMPLETED);
            Double turnover = orderMapper.sumByMap(map);
            turnover = turnover==null?0.0:turnover;
//            turnover = Math.random()*100 + 200;
            turnoverList.add(turnover);
        }
        System.out.println(turnoverList);
        return TurnoverReportVO.builder().
                dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        ArrayList<Integer> newUserList = new ArrayList<>();
        ArrayList<Integer> totalUserLIst = new ArrayList<>();

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            HashMap<Object, Object> map = new HashMap<>();
            map.put("end", endTime);
            Integer totalUsers = userMapper.countByMap(map);
            map.put("begin", beginTime);
            Integer newUsers = userMapper.countByMap(map);
            totalUserLIst.add(totalUsers);
            newUserList.add(newUsers);
        }

        return UserReportVO.builder().
                dateList(StringUtils.join(dateList, ",")).
                totalUserList(StringUtils.join(totalUserLIst, ",")).
                newUserList(StringUtils.join(newUserList, ",")).
                build();
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        ArrayList<Integer> totalOrder = new ArrayList<>();
        ArrayList<Integer> realOrder = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //查询每天订单数
            Integer eachDayOrders = getOrderCount(beginTime, endTime, null);
            //查询每天有效订单数
            Integer realOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
            totalOrder.add(eachDayOrders);
            realOrder.add(realOrderCount);
        }
        //计算订单总数
        Integer totalOrderCount = totalOrder.stream().reduce(Integer::sum).get();
        //计算有效订单数
        Integer validOrderCount = realOrder.stream().reduce(Integer::sum).get();
        //订单完成率
        Double orderCompRate;
        if(totalOrderCount == 0) {
            orderCompRate = 0.0;
        }else {
            orderCompRate = validOrderCount.doubleValue() / totalOrderCount;
        }
        return OrderReportVO.builder().
                dateList(StringUtils.join(dateList, ",")).
                orderCountList(StringUtils.join(totalOrder, ",")).
                validOrderCountList(StringUtils.join(realOrder, ",")).
                totalOrderCount(totalOrderCount).
                validOrderCount(validOrderCount).orderCompletionRate(orderCompRate).
                build();
    }

    private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", status);
        Integer integer = orderMapper.countByMap(map);
        return integer;
    }
}
